/*
*    Clean AJAX Engine v4.0
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
* Provides a class based on Iterator design pattern used to provide 
* abstraction from DOM elements.
* @author Carlos Eduardo Goncalves
*/

/**
* <p>Document object model iterator constructor.</p>
* @param doc
*		Document used to iterate in.
*/
function DomIterator(doc){
    /** <p>Document used by the iterator.</p> */	
	DomIterator.prototype.doc = doc;
}

/**
* <p>Search for an element on a document and change its value.</p>
* @param id
*		DOM element id attribute value.
* @param value
*		Value to be applied on element.
* @param replaceOld
*		Flag used to determine if the value must be concatenated to the 
*		old value or if the value must replace the old value.
*/
DomIterator.prototype.applyValue = function(id, value, replaceOld){
  try{
    var el = this.doc.getElementById(id);
	if(!el) return;
    var iframes = this.doc.getElementsByTagName("iframe");
    for(var i = 0; i <= iframes.length; ++i){
	  if(iframes[i] != null) {
  	    if(iframes[i].id == el.id){
  	        el.contentWindow.document.body.innerHTML = (replaceOld) ? value : el.contentWindow.document.body.innerHTML + value;
	        return;
	    }
	  }
    }
	if(el.value != null){
       el.value = (replaceOld) ? value : el.value + value;	  
	   return;
    }
	if(el.innerHTML != null){
       el.innerHTML = (replaceOld) ? value : el.innerHTML + value;	  
	   return;
    }
  } catch(e){Engine.reportException(null, e);}
}

/**
* <p>Search a form in the document, using the form id attribute as search key.</p>
* @param formId
*		<code>HTMLForm</code> id attribute value.
* @return
*		<code>HTMLForm</code> instance recovered.
*/
DomIterator.prototype.getFormById = function(formId){	
  var form = null;
  var forms = this.doc.getElementsByTagName("form"); 
  for(var i = 0; i <= forms.length; ++i){
    if(forms[i] != null) {
      if(forms[i].id == formId) {
	    form = forms[i];
	    break;
      }
    }
  }
  return form;
}

/**
* <p>Search a form in the document, using the form name attribute as search key.</p>
* @param formName
*		<code>HTMLForm</code> name attribute value.
* @return
*		<code>HTMLForm</code> instance recovered.
*/
DomIterator.prototype.getFormByName = function(formName){	
  var form = null;	
  var forms = this.doc.getElementsByTagName("form"); 
  for(var i = 0; i <= forms.length; ++i){
    if(forms[i] != null) {
      if(forms[i].name == formName) {
	    form = forms[i];
	    break;
      }
    }
  }	
  return form;
}